/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hhf.flex.ascreator.patterns;

import hhf.flex.annotations.services.RemoveEntities;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.util.Map;

/**
 *
 * @author utilisateur
 */
public class ASRemoveEntitiesPattern {

    /**
     * Ecrit les fonctions de type "removeEntity" qui permettent de supprimer
     * des entités de la base locale.
     * @param out
     * @param method
     * @param argsTyped
     * @param annotedNames
     * @throws IOException 
     */
    public static void writeRemoveEntitiesFunctions(OutputStreamWriter out, Method method, String argsTyped, Map<String, String> annotedNames) throws IOException {
        //il y a une annotation @RemoveEntities
        if (method.getAnnotation(RemoveEntities.class) != null) {
            out.write("\t\t//Fonction @RemoveEntities\n");
            out.write("\t\tprivate function removeEntities(" + argsTyped + "):void {\n");
            RemoveEntities removeEntities = method.getAnnotation(RemoveEntities.class);
            for (String entityName : removeEntities.entities().split(",")) {
                if (annotedNames.get(entityName) != null) {
                    out.write("\t\t\tem.remove(" + annotedNames.get(entityName) + ");\n");
                } else {
                    //TODO :  gestion de l'erreur
                }
            }
            out.write("\t\t}\n");
        }
    }
}
